﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using System.Linq;
using System.Data.Linq;
using System.Data.Linq.WorkAround;
using Microsoft.Phone.Data.Linq;

namespace FY13
{
    public class FYApplicationService : IApplicationService
    {
        private const int DBVersion = 1;
        private const string DBVerison_String = "DBVersion";



        public void StartService(ApplicationServiceContext context)
        {
            Microsoft.Phone.Controls.TiltEffect.TiltableItems.Add(typeof(StackPanel));




            using (FY13.Data.FYDataBase db = new Data.FYDataBase())
            {
                if (!db.DatabaseExists())
                {
                    db.CreateDatabase();
                    DatabaseSchemaUpdater dsu = db.CreateDatabaseSchemaUpdater();
                    dsu.DatabaseSchemaVersion = DBVersion;
                    dsu.Execute();
                    var str = GetStreamString("UserInfo.json");
                    db.Users.InsertAllOnSubmit(G.Utils.SerializerHelp.JsonDeserialize<FY13.Data.UserInfo[]>(str));
                    db.SubmitChanges();
                }
            }

            if (!System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings.Contains("Version"))
            {
                foreach (var item in ViewModel.ReminderViewMoel.Reminders)
                {
                    string name = Guid.NewGuid().ToString();
                    Microsoft.Phone.Scheduler.ScheduledActionService.Add(new Microsoft.Phone.Scheduler.Reminder(name) { BeginTime = item.Time.AddMinutes(-15), Title = "Reminder", Content = item.Content });
                }
                System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings["Version"] = 1.0;
            }
            //下个版本需要清除前一版本的提醒
            //string name = Guid.NewGuid().ToString();
            //Microsoft.Phone.Scheduler.ScheduledActionService.Add(new Microsoft.Phone.Scheduler.Reminder(name) { BeginTime = DateTime.Now.AddSeconds(30), Title = "ceshi", Content="ghwghw" });
        }
        private string GetStreamString(string path)
        {
            var stream = App.GetResourceStream(new Uri(string.Format("/FY13;component/Resource/Content/{0}", path), UriKind.Relative)).Stream;
            return stream.ReadToEnd();
        }
        public void StopService()
        {
        }
    }


}
